package com.nic.view.widget;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nic.view.R;
import com.nic.view.adapter.CommonCheckAdapter;
import com.nic.view.adapter.MultChooseAdapter;
import com.nic.view.model.CommonChooseBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/13.
 */

public class ComMultPopWindow extends CommonPopupWindow implements View.OnClickListener, CommonCheckAdapter.OnClickItemMultiListener {
    private TextView tv_shadow;
    private MyGridView gv;
    private Button bt_cancel;
    private Button bt_confirm;


    private TextView tvDes;
    private MultChooseAdapter adapter;
    private String currtIds;
    private String currtNames;
    private int position;//区分多个多选弹出框
    private OnMultiListener mOnMultiListener;
    private CheckBox cbAll;
    private TextView tvLoadMore;
    private int pageIndex;

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public void setLoadMore(boolean loadMore) {
        if (loadMore) {
            tvLoadMore.setVisibility(View.VISIBLE);
            tvLoadMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    loadMore();
                }
            });
        }
    }

    private List<CommonChooseBean> loadMoreList = new ArrayList<>();

    public ComMultPopWindow(Context c) {
        super(c, R.layout.pop_mult, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        init();
    }

    public void setOnMultiListener(OnMultiListener mOnMultiListener) {
        this.mOnMultiListener = mOnMultiListener;
    }

    public void init() {
//        getPopupWindow().setOutsideTouchable(false);
        cbAll = (CheckBox) contentView.findViewById(R.id.cb_all);
        cbAll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (adapter.getList().isEmpty()) {
                    return false;
                }
                if (!cbAll.isChecked()) {
                    adapter.selectedAll();
                } else {
                    adapter.unSelected();
                    clearPopState();
                }
                return false;
            }
        });
        tv_shadow = (TextView) contentView.findViewById(R.id.tv_shadow);
        tv_shadow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearPopState();
                getPopupWindow().dismiss();
            }
        });
        tvDes = (TextView) contentView.findViewById(R.id.tv_desc);
        gv = (MyGridView) contentView.findViewById(R.id.gv);
        bt_cancel = (Button) contentView.findViewById(R.id.cancel);
        bt_cancel.setOnClickListener(this);
        bt_confirm = (Button) contentView.findViewById(R.id.confirm);
        bt_confirm.setOnClickListener(this);
        tvLoadMore = (TextView) contentView.findViewById(R.id.tv_load_more);
        // 设置弹出窗体需要软键盘，
        getPopupWindow().setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        // 再设置模式，和Activity的一样，覆盖，调整大小。
        getPopupWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        adapter = new MultChooseAdapter();
        adapter.setClickItemMultiListener(this);
        gv.setAdapter(adapter);

    }

    public void setDesTitle(String text) {
        if (TextUtils.isEmpty(text)) {
            return;
        }
        tvDes.setText("" + text);
    }


    public void setList(List<CommonChooseBean> list) {
        adapter.setList(list);
    }

    public void addAll(List<CommonChooseBean> list) {
        adapter.addAll(list);
    }

    public List<CommonChooseBean> getList() {
        return adapter.getList();
    }

    public void showAsDropDown(View view, int position) {
        if (!getPopupWindow().isShowing() && !((Activity) context).isFinishing()) {
            this.position = position;
            cbAll.setChecked(adapter.checked(currtIds));
            getPopupWindow().showAsDropDown(view);
        }
    }

    public void showAtLocation(View view, int position) {
        if (!getPopupWindow().isShowing() && !((Activity) context).isFinishing()) {
            this.position = position;
            cbAll.setChecked(adapter.checked(currtIds));
            getPopupWindow().showAtLocation(view, Gravity.CENTER, 0, 0);
        }
    }

    public void show(int position) {
        if (!getPopupWindow().isShowing() && !((Activity) context).isFinishing()) {
            this.position = position;
            cbAll.setChecked(adapter.checked(currtIds));
            getPopupWindow().showAtLocation(((Activity) context).getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        }
    }

    public boolean isShow() {
        return getPopupWindow().isShowing();
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.cancel) {
            clearPopState();
            getPopupWindow().dismiss();

        } else if (i == R.id.confirm) {
            getPopupWindow().dismiss();
            if (mOnMultiListener != null) {
                mOnMultiListener.onMultiItem(currtIds, position);
            }
        }

    }

    public void clearPopState() {
        this.currtIds = "";
        this.currtNames = "";
        adapter.unSelected();
    }


    public String getCNames() {
        return currtNames;
    }

    public String getCIds() {
        return currtIds;
    }

    @Override
    public void checkedItem(Map<String, String> map) {
        if (map.size() == adapter.getCount()) {
            cbAll.setChecked(true);
        } else {
            cbAll.setChecked(false);
        }
        if (map.isEmpty() || map.size() == adapter.getCount()) {
            this.currtIds = "";
            this.currtNames = "";
            return;
        }
        this.currtIds = TextUtils.join(",", map.keySet());
        this.currtNames = TextUtils.join(",", map.values());
    }

    public void setLoadMoreList(List<CommonChooseBean> loadMoreList) {
        this.loadMoreList = loadMoreList;
        setLoadMore(true);
        loadMore();//
    }

    /**
     * 每次截取100条
     */
    private void loadMore() {
        try {
            if (getList().size() + 100 >= loadMoreList.size()) {
                addAll(loadMoreList.subList(getList().size(), loadMoreList.size()));
                tvLoadMore.setVisibility(View.GONE);
            } else {
                addAll(loadMoreList.subList(pageIndex * 100, pageIndex * 100 + 100));
            }
            pageIndex++;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface OnMultiListener {
        void onMultiItem(String ids, int position);
    }

    public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
        getPopupWindow().setOnDismissListener(listener);
    }
}
